<?php
/***
	pms表。
***/
class Pdms_pms extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('bid_project_model');
		$this->load->model('pdms_pms_model');
		$this->load->model('pdms_type_model');
		$this->load->model('pdms_shapcode_model');
		$this->load->model('pdms_standard_model');
		$this->load->model('pdms_subtype_model');
		$this->load->model('pdms_contype_model');
		$this->load->model('pdms_classthk_model');
		$this->load->model('pdms_size_model');
		$this->load->model('Pdms_materialcode_model');
		$this->load->helper('url');
	}
	
	/***
	新增、删除一体化设计；
	相比料单，这个记录条目还不算多，局域网带宽就是任性。
	
	***/
	public function index($project_id = 0, $id = 0, $flag_del = 0, $pipe_class = NULL) {
			
		//如果POST form1表示传来项目编号，就要列选项了。
		if (isset($_POST['form1'])) {
			$project_id = $_POST['project_id'];
			$pipe_class = $_POST['pipe_class'];
			$data['project_id'] = $project_id; //这个要输入到pms表里的
			$data['pipe_class'] = $pipe_class;
			$project = $this->bid_project_model->get_by_id_code_name($project_id);
			$data['project_code'] = $project['code'];
			$data['project_name'] = $project['name'];
			
			//类别、连接方式、壁厚等级、尺寸是预先加载
			$data['types'] = $this->pdms_type_model->get_all( );
			$data['contypes'] = $this->pdms_contype_model->get_all( );
			$data['classthks'] = $this->pdms_classthk_model->get_all( );
			$data['sizes'] = $this->pdms_size_model->get_all( );
			
		}
		
		//如果POST form2表示新建一条PMS数据
		if (isset($_POST['form2'])) {
			$this->pdms_pms_model->create();
			
			$pipe_class = $_POST['pipe_class'];
			$project_id = $_POST['project_id'];
			$data['project_id'] = $project_id; //这个要输入到pms表里的
			$data['pipe_class'] = $pipe_class;
			$project = $this->bid_project_model->get_by_id_code_name($project_id);
			$data['project_code'] = $project['code'];
			$data['project_name'] = $project['name'];
			
			//类别、连接方式、壁厚等级、尺寸是预先加载
			$data['types'] = $this->pdms_type_model->get_all( );
			$data['contypes'] = $this->pdms_contype_model->get_all( );
			$data['classthks'] = $this->pdms_classthk_model->get_all( );
			$data['sizes'] = $this->pdms_size_model->get_all( );
						
		}
		
		//如果$flag_del<>0，要去删除一条记录
		if ($flag_del <> 0) {
			$this->pdms_pms_model->del($id);
			$data['project_id'] = $project_id;
			$data['pipe_class'] = $pipe_class;
			
			$project = $this->bid_project_model->get_by_id_code_name($project_id);
			$data['project_code'] = $project['code'];
			$data['project_name'] = $project['name'];
			
			//类别、连接方式、壁厚等级、尺寸是预先加载
			$data['types'] = $this->pdms_type_model->get_all( );
			$data['contypes'] = $this->pdms_contype_model->get_all( );
			$data['classthks'] = $this->pdms_classthk_model->get_all( );
			$data['sizes'] = $this->pdms_size_model->get_all( );
			
		}
		
		$data['projects'] = $this->bid_project_model->get_code_name();
		$data['title'] = "管道等级表";
		
		$this->load->library('parser');
		
		$this->load->view('templates/pdms_head');
		$this->load->view('templates/pdms_menu');
		$this->parser->parse('pdms_pms/form_project',$data);
		
		if ($project_id <> 0) {
		
			$this->parser->parse('pdms_pms/form_create', $data);
		
			if ($pipe_class <> NULL) {
				$data['pmss'] = $this->pdms_pms_model->get_by_project_class($project_id, $pipe_class);
				$this->parser->parse('pdms_pms/list', $data);
			}
		}
		$this->load->view('templates/foot');
		
	}
	
	//导出部分
	public function export_txt($project_id, $pipe_class ) {
	
	//删除文件，以追加的方式打开文件
	unlink("./temp/pms.txt");
	@$fp=fopen("./temp/pms.txt", "a+");
	
	//输出等级表的开头部分
	fwrite($fp, "$S-\r\n");
	fwrite($fp, "$(  BORE UNITS MM    $)\r\n");
	fwrite($fp, "$(  BORE UNITS MM    $)\r\n");
	fwrite($fp, "\r\n");
	
	fwrite($fp, "NEW SPECIFICATION /".$pipe_class."\r\n");
	fwrite($fp, "VERSION 1151\r\n");
	fwrite($fp, "MATREF =0\r\n");
	fwrite($fp, "FLUREF =0\r\n");
	fwrite($fp, "RATING 0.000\r\n");
	fwrite($fp, "LINETYPE NULL\r\n");
	fwrite($fp, "BSPEC =0\r\n");
	fwrite($fp, "NOMREF =0\r\n");
	fwrite($fp, "BRREF =0\r\n");
	fwrite($fp, "REDREF =0\r\n");
	fwrite($fp, " \r\n");
	fwrite($fp, "TEXT \'PIPING\'\r\n");
	fwrite($fp, "\r\n");
	fwrite($fp, "HEADING\r\n");
	
	//去搜这个project_id的这个pipe_class，再把它解开
	$classes = $this->pdms_pms_model->get_by_project_class($project_id, $pipe_class);
	foreach ($classes as $class) {
		$head_code = $class['head_code'];
		$shap_code = $class['shap_code'];
		$material_code = $class['material_code'];
		$min_dn = $class['min_dn'];
		$max_dn = $class['max_dn'];
		
		//根据head_code获取一串hending过来
		
	}
	
	//输出末尾
	fwrite($fp, "\r\n");
	fwrite($fp, "$S+\r\n");
	//关闭文件
	fclose($fp);
	
	//加载下载辅助函数，并下载文件
	$this->load->helper('download');
	force_download('./temp/pms.txt', NULL);
	
	}
	//异步输出部分
	public function get_standard( ) {
		$type_code = $_POST['type_code'];
		
		$standards = $this->pdms_standard_model->get_by_type($type_code);
		$back_string = "<option value='__'>请选择</option>";
		foreach ($standards as $standard) {
			$back_string = $back_string."<option value = '".$standard['code']."'>".$standard['des']."</option>";
		}
		echo $back_string;
	}
	
	public function get_subtype( ) {
		$type_code = $_POST['type_code'];
		
		$subtypes = $this->pdms_subtype_model->get_by_type($type_code);
		$back_string = "<option value='_'>请选择</option>";
		foreach ($subtypes as $subtype) {
			$back_string = $back_string."<option value = '".$subtype['code']."'>".$subtype['des']."</option>";
		}
		echo $back_string;
	}
	
	public function get_material( ) {
		$type_code = $_POST['type_code'];
		
		$materials = $this->Pdms_materialcode_model->get_by_type($type_code);
		$back_string = "<option value='____'>请选择</option>";
		foreach ($materials as $material) {
			$back_string = $back_string."<option value = '".$material['code']."'>".$material['des']."</option>";
		}
		echo $back_string;
	}
	
	public function get_pipe_class( ) {
		$project_id = $_POST['project_id'];
		$back_string = "<option value='NULL'>请选择管道等级</option>";
		$pmses = $this->pdms_pms_model->get_class_by_project($project_id);
		
		foreach ($pmses as $pms) {
			$back_string = $back_string."<option value = '".$pms['pipe_class']."'>".$pms['pipe_class']."</option>";
		}
		echo $back_string;
	}
}